package com.scm.controller;

/**
 * @author CHL
 * @ClassName: CategoryController
 * @Description: TODO
 * @date 2022/3/8 14:21
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.CategoryMapper;
import com.scm.entity.Category;
import com.scm.entity.response.ResponseMessage;
import com.scm.util.StringUtil;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/sell"})
public class CategoryController
{
    @Autowired
    private CategoryMapper categoryMapper;
    private ResponseMessage rm = new ResponseMessage();

    private boolean checkCategory(Category category)
    {
        Integer categoryId = category.getCategoryId();
        if (StringUtil.isEmpty(categoryId))
        {
            this.rm.setMessage("产品类别序列号categoryId参数：不能为空");
            return false;
        }
        String name = category.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setMessage("产品类别名称name参数：不能为空");
            return false;
        }
        return true;
    }

    @RequestMapping({"/category/add"})
    public ResponseMessage categoryAdd(Category category)
    {
        String name = category.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("产品类别名称name参数：不能为空");
            return this.rm;
        }
        this.categoryMapper.addCategory(category);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("添加产品分类成功");

        return this.rm;
    }

    @RequestMapping({"/category/delete"})
    public ResponseMessage delete(Integer categoryId)
    {
        if (categoryId == null)
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("缺少必要参数：categoryId");
        }
        else
        {
            int n = this.categoryMapper.deleteCategory(categoryId);
            if (n == 0)
            {
                this.rm.setCode(Integer.valueOf(4));
                this.rm.setMessage("删除失败");
            }
            else
            {
                this.rm.setCode(Integer.valueOf(2));
                this.rm.setMessage("删除成功");
                this.rm.setData(this.categoryMapper.select(null));
            }
        }
        return this.rm;
    }

    @RequestMapping({"/category/update"})
    public ResponseMessage update(Category category)
    {
        if (!checkCategory(category))
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        int n = this.categoryMapper.updateCategory(category);
        if (n == 0)
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("修改失败");
        }
        else
        {
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("修改成功");
        }
        return this.rm;
    }

    @RequestMapping({"/category/show"})
    public PageInfo<Category> select(Category category, Integer page)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<Category> categorys = this.categoryMapper.select(category);
        PageInfo<Category> info = new PageInfo(categorys);
        return info;
    }

    @RequestMapping({"/category/all"})
    public List<Category> selectAll()
    {
        List<Category> categorys = this.categoryMapper.select(null);
        return categorys;
    }
}

